#!/usr/bin/python
# coding = "utf-8"
import json
import time
import os
import Assembly_Clock

# import Assembly_Email

# 為善言小姐姐寫的自動打卡程序

# 设置自动打卡轮回时间，目前禁用
# def sleep_time(hour, min, sec):
#     return hour * 3600 + min * 60 + sec


# main
isFirst = True
print('---' + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + '---' + "Layer 1 Defense System Activated")
while True:
    # 间隔秒数second
    # second = sleep_time(0, 0, 5)
    # time.sleep(second)
    # 刷新
    time_now = time.strftime("%H:%M:%S", time.localtime())
    wrongFlag2 = 0
    if time_now == "09:00:50":
        fans = open("data.json", "r", encoding="utf-8-sig")
        text_data = fans.read()
        if text_data.startswith(u'\ufeff'):
            text_data = text_data.encode('utf8')[3:].decode('utf8')
        fanText = json.loads(text_data)
        user = fanText["user"]
        for i in user:
            result1 = Assembly_Clock.clockIn(i['name'], i['number'], i['content'])
            if result1 == 500:
                wrongFlag2 = 1
            time.sleep(1)
        fans.close()
    # 二级防御
    if wrongFlag2 == 1 & isFirst:
        os.system('python Next.py')
        isFirst = False
        time.sleep(7 * 24 * 3600)  # 防止循环执行Next.py
